﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Cangku.cy
{
    public partial class XiuGai : Form
    {
        public int bh;
        public string yhxm;
        public string yhmm;
        public string yhqx;
        public XiuGai()
        {
            InitializeComponent();
        }

        private void qued_Click(object sender, EventArgs e)
        {
            string ygbh = this.YGBH.Text.Trim();
            string xm = this.YGXM.Text.Trim();
            string mm = this.YGMM.Text.Trim();
            string qx = this.YGQX.Text.Trim();
            if (yhxm!=xm||yhmm!=mm||yhqx!=qx)
            {
                if (qx == "管理员" || qx == "普通用户")
                {
                    DBHelper db = new DBHelper();
                    string sql = string.Format("update [User] set Username='{0}',Userpassword='{1}',UserType='{2}' where UserID='{3}'", xm, mm, qx, ygbh);
                    try
                    {
                        bool result = db.ExcuteSql(sql);
                        if (result)
                        {
                            MessageBox.Show("修改成功！");
                            YGXX ower = (YGXX)this.Owner;
                            ower.LoadUser();
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("修改失败！");
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("操作失败！请重试！！！");
                        YGMM.Clear();
                        YGXM.Clear();
                        throw;
                    }

                }
                else
                {
                    MessageBox.Show("请输入正确的用户权限");
                }
            }
            else
            {
                MessageBox.Show("请输入修改内容！");
            }
            
            
            
        }

        private void XiuGai_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=CK;Integrated Security=True");
            string sql = string.Format("select * from [User] where UserID='{0}'", bh);
            SqlDataAdapter dap = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            dap.Fill(dt);
            this.YGBH.Text = dt.Rows[0]["UserID"].ToString();
            yhxm = this.YGXM.Text = dt.Rows[0]["Username"].ToString();
            yhmm = this.YGMM.Text = dt.Rows[0]["Userpassword"].ToString();
            yhqx = this.YGQX.Text = dt.Rows[0]["UserType"].ToString();
        }

        private void qux_Click(object sender, EventArgs e)
        {
            
            YGMM.Clear();
            YGXM.Clear();
        }
    }
}
